package cn.school.newExam.service.impl;

import cn.school.common.utils.StringUtils;
import cn.school.common.utils.SysUserUtils;
import cn.school.newExam.dao.ExamPlanMapper;
import cn.school.newExam.entity.ExamPlan;
import cn.school.newExam.service.ExamPlanService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class ExamPlanServiceImpl extends ServiceImpl<ExamPlanMapper, ExamPlan> implements ExamPlanService {

    @Resource
    private ExamPlanMapper examPlanMapper;

    @Override
    public IPage<ExamPlan> queryPage(ExamPlan dto) {
        LambdaQueryWrapper<ExamPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(dto.getExamName()),ExamPlan::getExamName,dto.getExamName());
        queryWrapper.eq(dto.getExamTerm() != null,ExamPlan::getExamTerm,dto.getExamTerm());
        queryWrapper.eq(ExamPlan::getTenantId,SysUserUtils.getSysUser().getTenantId());
        Page<ExamPlan> page = page(new Page<>(dto.getPageNum(),dto.getPageSize()),queryWrapper);
        return page;
    }

    @Override
    public ExamPlan getExamPlanById(String id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public int add(ExamPlan examPlan) {
//        Long userId = SysUserUtils.getSysUser().getId();
        examPlan.setCreateBy(SysUserUtils.getSysUser().getUsername());
        examPlan.setCreateTime(new Date());
        return this.baseMapper.insert(examPlan);
    }

    @Override
    public int update(ExamPlan examPlan) {
        Long userId = SysUserUtils.getSysUser().getId();
        examPlan.setModifyBy(userId.toString());
        examPlan.setModifyTime(new Date());
        return this.baseMapper.updateById(examPlan);
    }

    @Override
    public int delete(Integer[] ids) {
        return examPlanMapper.batchDelete(ids);
    }

    @Override
    public ExamPlan queryExamPlan(ExamPlan dto) {
        LambdaQueryWrapper<ExamPlan> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(dto.getExamName()),ExamPlan::getExamName,dto.getExamName());
        queryWrapper.eq(StringUtils.isNotEmpty(dto.getExamStartDay()),ExamPlan::getExamStartDay,dto.getExamStartDay());
        queryWrapper.eq(StringUtils.isNotEmpty(dto.getExamEndDay()),ExamPlan::getExamEndDay,dto.getExamEndDay());
        queryWrapper.eq(StringUtils.isNotEmpty(dto.getTenantId()),ExamPlan::getTenantId,SysUserUtils.getSysUser().getTenantId());
        return this.baseMapper.selectOne(queryWrapper);
    }
}
